﻿<cfscript>
/** 
* acadmicBase - ClassroomBorrow, ClassroomTimeTableForBorrow - 教室借用相关控制器
* 
* @extends "MachII.framework.Listener"
* 
* @displayname "acadmicManager.filters.senate.courseAccessFilter" 
* @hint "acadmicBase - ClassroomBorrow, ClassroomTimeTableForBorrow - 教室借用相关控制器" 
* 
* @Output false
* 
* @depends "sessionAdvice, datetimeAdvice, senateMathAdvice, sequenceClassroomTimeTable, sequenceClassroomBorrow"
*/ 

component {
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 

	/**
	* @hint "取消教室借用记录"
	* 
	* @event "MachII Event Object"
	*/
	public void function cancelClassroomSign() output=true {
		
		var sessionAdvice = getsessionAdvice();
		
		/* 当前日期时间字符串 */
		var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
		
		/* 审批人 姓名(工号) */
		var approver = sessionAdvice.getUserProp("userName") & "(" & sessionAdvice.getAutherUserID() & ")";
		
		var docId = event.getArg("DOCID");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 更改借用记录状态 */
			sql = "UPDATE t_clsroom_borrow 
					SET 
						proc_stat = 'C', 
						time_cancel = :currentDateTime, 
						tch_cancel = :approver 
					WHERE 
						cbr_id = :docId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="docId", value=docId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="approver", value=approver, cfsqltype="cf_sql_varchar" );

			queryObj.execute( sql=sql );
			
			/* 查找时间占用 */
			sql = "SELECT crtime_id FROM t_clsroom_forborrow WHERE cbr_id = :docId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="docId", value=docId, cfsqltype="cf_sql_char" );

			var rs = queryObj.execute( sql=sql ).getResult();
			
			for ( var a=1; a LTE rs.recordCount; a++ ) {
				
				var timeTableId = rs["crtime_id"][a];
				
				/* 删除教室时间占用-借用扩展记录 */ 
				sql = "DELETE FROM t_clsroom_forborrow WHERE crtime_id = :timeTableId ";
			
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="timeTableId", value=timeTableId, cfsqltype="cf_sql_char" );

				queryObj.execute( sql=sql );
			
				/* 删除教室时间占用记录 */
				sql = "DELETE FROM t_clsroom_timetable WHERE crtime_id = :timeTableId ";
			
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="timeTableId", value=timeTableId, cfsqltype="cf_sql_char" );

				queryObj.execute( sql=sql );
				
			}

		}
		
	}
	
	/**
	* @hint "添加并直接审批教室借用记录"
	* 
	* @event "MachII Event Object"
	*/
	public void function createProvedClassroomSign() output=true {
		
		var mathAdvice = getsenateMathAdvice();
		var sessionAdvice = getsessionAdvice();
		
		var sequenceTimeTable = getsequenceClassroomTimeTable();
		var sequenceSign = getsequenceClassroomBorrow();
		
		/* 当前日期时间字符串 */
		var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
		
		/* 学期代码 */
		var termId = event.getArg("TermID");
		/* 使用类别 */
		var usageType = event.getArg("UsageType");
		
		/* 起始周 */
		var w1 = event.getArg("W1");
		/* 结束周 */
		var w2 = event.getArg("W2");
		/* 星期几 */
		var wkday = event.getArg("WKDAY");
		/* 时段 */
		var wkcell = event.getArg("CELL");
		/* 教室列表 使用 | 分割 */
		var roomIds = listToArray( event.getArg("ROOMIDS"), "|" );
		
		/* 时段长度 */
		var cellLength = 2;
		switch ( wkcell ) {
			
			case 'S':  /* 上午 */
				cellLength = 5; 
				break;
			
			case 'X': /* 下午 */
			case 'W': /* 晚上 */
				cellLength = 4; 
				break;

			case 'B': /* 白天 */
				cellLength = 9; 
				break;
			
			case 'Q': /* 全天 */
				cellLength = 13; 
				break;
		}
		
		/* 转换周次 01-20 */
		var weekStr = numberFormat( w1, "0_" ) & "-" & numberFormat( w2, "0_" );
		
		/* 转换周次序列 1111111111111111111 */
		var weekIdx = mathAdvice.convertWeekIdx( weekStr );
		
		/* 转换节次起点 */
		var cellStart = mathAdvice.convertLectureIdxToCellStart( wkcell );
		
		/* 审批人 姓名(工号) */
		var approver = sessionAdvice.getUserProp("userName") & "(" & sessionAdvice.getAutherUserID() & ")";
		/* 使用时间描述 */
		var useTime = event.getArg("TimeDescription");
		/* 使用地点描述 */
		var useRoom = event.getArg("RoomDescription");
		
		/* 申请教师 */
		var teacherId = event.getArg("TeacherID");
		/* 申请部门 */
		var instituteId = event.getArg("InstituteID");
		/* 申请人 姓名(工号) */
		var signer = event.getArg("Signer");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {

			/* 教室借用记录日志 */
			var signId = sequenceSign.getID();
			
			sql = "INSERT INTO t_clsroom_borrow 
					( 
					cbr_id, time_sign, time_review, tch_approve, use_week, use_time, use_rooms, proc_stat, 
					tch_id, institute_id, ut_id, term_id
					) 
					VALUES 
					(
					:signId, :currentDateTime, :currentDateTime, :approver, :weekStr, :useTime, :useRoom, 'S', 
					:teacherId, :instituteId, :usageType, :termId
					) ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="signId", value=signId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="approver", value=approver, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="weekStr", value=weekStr, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="useTime", value=useTime, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="useRoom", value=useRoom, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="teacherId", value=teacherId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="instituteId", value=instituteId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="usageType", value=usageType, cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );
			
			
			
			/* 教室时间占用记录 */
			for ( var roomId in roomIds ) {
				
				/* 生成主键 */
				sequenceTimeTable.setTermID( termId );
				sequenceTimeTable.setClassroomID( roomId );
				
				var timeTableIdx = sequenceTimeTable.getIDX();
				
				sequenceTimeTable.setIndex( timeTableIdx );
				
				var timeTableId = sequenceTimeTable.getID();
				
				/* 教室时间占用 */
				sql = "INSERT INTO t_clsroom_timetable 
						( crtime_id, uidx, widx, wdidx, cellidx, cellrelay, term_id, cr_id, ut_id ) 
						VALUES 
						( :timeTableId, :timeTableIdx, :weekIdx, :wkday, :cellStart, :cellLength, :termId, :roomId, :usageType ) ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="timeTableId", value=timeTableId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="timeTableIdx", value=timeTableIdx, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="weekIdx", value=weekIdx, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="wkday", value=wkday, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="cellStart", value=cellStart, cfsqltype="cf_sql_interger" );
				queryObj.addParam( name="cellLength", value=cellLength, cfsqltype="cf_sql_interger" );
				queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="roomId", value=roomId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="usageType", value=usageType, cfsqltype="cf_sql_char" );

				queryObj.execute( sql=sql );

				/* 教室时间占用-借用扩展 */ 
				sql = "INSERT INTO t_clsroom_forborrow 
						( crtime_id, cbr_id, tch_sign, tch_approve ) 
						VALUES 
						( :timeTableId, :signId, :signer, :approver ) ";
				
				queryObj.addParam( name="timeTableId", value=timeTableId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="signId", value=signId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="signer", value=signer, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="approver", value=approver, cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
			}
			
		}
		
		/* Pass Args */
		event.setArg("DOCID", signId);

	}
	
}
</cfscript>